function LoginCtrl($scope, $http) {
	$scope.user = {};
	
	$scope.init = function () {
		$("#username").focus();
	};

	$scope.init();
	
	$scope.login = function () {
		if ($scope.isUsernameValid() && $scope.isPasswordValid()) {
			$http.post("/login", $scope.user).success(function (response) {
				if (response.success) {
					location.href='/main';
				} else {
					$scope.loginError = response.message;
				}
			});
		}
	};

	$scope.sendMail = function() {
		if ($scope.isUsernameValid()) {
			$http.post("/login/sendMail/"+ $scope.user.username).success(function (response) {
				if (response.success) {
					$scope.loginError = "密码找回邮件已发送到您的注册邮箱，请按照邮件提示修改密码。";
				} else {
					$scope.loginError = response.message || "操作失败！";
				}
			});
		}
	};
	
	$scope.isUsernameValid = function(){
		$scope.loginError = '';
		if (!$scope.user.username) {
			$scope.loginError = '请输入用户名';
			$("#username").focus();
			return false;
		} else {
			return true;
		}
	};
	
	$scope.isPasswordValid = function(){
		$scope.loginError = '';
		if (!$scope.user.password) {
			$scope.loginError = '请输入密码';
			$("#password").focus();
			return false;
		} else {
			return true;
		}
	};
}

app.controller('LoginCtrl', LoginCtrl);